Imports NHibernate.Engine
Imports KanBan.Pgd.Domain.Entities
Imports NHibernate
Imports System.Text

Namespace Repositories

    Public Class CandidateRepository
        Inherits RepositoryBase(Of Candidate)

        Public Function Query(ByVal name As String, Optional ByVal dni As String = "") As IEnumerable(Of Candidate)

            Try

                Dim session As ISession = NHibernateHelper.GetCurrentSession()
                Dim results As IList(Of Candidate) = New List(Of Candidate)

                Dim hqlQuery As New StringBuilder("from Candidate c where c.IsDeleted = false")

                If name <> String.Empty Then
                    hqlQuery.Append(String.Format(" and c.Name = '{0}'", name))
                End If

                If dni <> String.Empty Then
                    hqlQuery.Append(String.Format(" and c.Dni = {0}", dni))
                End If

                results = session.CreateQuery(hqlQuery.ToString()) _
                            .List(Of Candidate)()

                Return results

            Catch ex As Exception

                Throw

            End Try

        End Function

        Public Function QueryCandidates(ByVal text As String) As IList(Of String)

            Try

                Dim session As ISession = NHibernateHelper.GetCurrentSession()
                Dim results As IList(Of String) = New List(Of String)

                Dim hqlQuery As String = "select distinct c.Name from Candidate c where " _
                                        + "c.IsDeleted = false and c.Name like :Text"

                results = session.CreateQuery(hqlQuery) _
                        .SetString("Text", text + "%") _
                        .List(Of String)()

                Return results

            Catch ex As Exception

                Throw

            End Try

        End Function

    End Class

End Namespace
